<?php
class Uuch_HltAPI {
     const AUTHTOKEN = '813cd34ba60e402a99db32dbec9277e8';  
     //const AUTHTOKEN = '0ee02d147f1e4cbc882e3fd42e826f2c'; //测试账号/密码：public1/123456
    //在线订单操作
    const ORDERS_OPERATION_URLS = 'http://api.goto56.com:8086/apic/services/order?wsdl';

     public static function CreatedBillByWayId($id,$channer)
    {
        header ( "content-type:text/html;charset=utf-8" );
        $billItem = Application_Model_wayBillItem::load($id);
        
        if(!$billItem){
            return array("type"=>0);
        }
        $invoiceItem = Application_Model_invoicedetail::bybillNo($billItem->billNo);
        if(!$invoiceItem){
            return array("type"=>2);
        }
        $productWeight = $billItem->getWeightByType(Application_Model_WeightType::ProductWeight);
        $weight = number_format(($productWeight/1000),2);

        $invoiceArr = array();
        foreach ($invoiceItem as $invoiceVal) {
            $ename = explode("（", $invoiceVal->Description);
            $ename = explode("(", $invoiceVal->Description);
            //判断中文    
            if (preg_match("/[\x7f-\xff]/", $ename[0],$rname)) { 
            $yname= strstr($ename[0],$rname[0],-1);
            $zname=strstr($ename[0],$rname[0]);
            }else{ 
            $yname= $ename[0];
            $zname='';
            }     
            $invoiceArr[] = array(
                "netWeight" => $weight,
                "pieces" => $invoiceVal->Num,//件数(默认: 1)
                "rebate" => 'N',//是否退税（是：Y，否：N）。
                "name" =>  $yname,//海关申报英文品名
                "cnName" =>$zname,//海关申报中文品名
                "unitPrice" => $invoiceVal->Price,//单价 0 < Amount <= [10,2]【***】
            );
        }

        $channerItem = Application_Model_ExpressChannel::load($channer);
        
        
        $arrs = array(
               "orderNo" => $billItem->BillNo,//客户订单号码，由客户自己定义【***】
                "transportWayCode" => $channerItem?$channerItem->Key:"",//运输方式代码
                "cargoCode" => 'W',//货物类型。取值范围[W:包裹/D:文件]
                "originCountryCode" => 'CN',//起运国家二字代码，参照国家代码表【***】
                "destinationCountryCode" => $billItem->CityBy->Word,//目的国家二字代码，参照国家代码表
                "pieces" => '1',//货物件数(默认：1) 0 < Amount <= [10,2] 
                "shipperCompanyName" => "Eyuki Commerce",//发件人公司名称 
                "shipperName" => "Eyuki Commerce",//发件人姓名 
                "shipperAddress" => "No.3 Langshan 2nd Street,Area of Hi-Tech Industrial Zone,Nanshan District, SHENZHEN",//发件人地址 
                "shipperTelephone" => "075588605979",//发件人电话号码               
                "shipperPostcode" =>  "",//发件人邮编
                "consigneeCompanyName" => $billItem->consignee,//收件人公司名称
                "consigneeName" => $billItem->consignee,//收件人公司名称姓名【***】
                "street" => $billItem->conAdd,//街道【***】
                "city" => "",//城市 【***】
                "province" => '',//州  /  省 【***】
                "consigneePostcode" => $billItem->conPostCode,//收件人邮编
                "consigneeTelephone" => $billItem->conTel,//收件人电话号码
                "weight" => $weight,//客户自己称的重量(单位：KG) 
                "insured" => 'N',//购买保险（投保：Y，不投保：N）。
                "goodsCategory" => 'G',//物品类别。取值范围[G:礼物/D:文件/S:商业样本/R:回货品/O:其他]
                "goodsDescription" => '',//物品类别内容。
                "memo" => '',//备注。
                "battery" => 'N',//是否带电池（是：Y，否：N）。
                "declareItems" => $invoiceArr
        );

        $soap = new SoapClient(self::ORDERS_OPERATION_URLS, array ('encoding' => 'UTF-8' ));
        //创建并预报订单 
        $result = $soap->createAndAuditOrder(self::AUTHTOKEN,$arrs);
        //$result = $soap->createOrder(self::AUTHTOKEN,$arrs);            
        if($result->success){
            return array("type"=>1,"agentNo" =>$result->trackingNo);
        } else {
            return array("type"=>0,"message"=>'错误:'.$result->error->errorInfo);
        }
    }

      public static function calculateCharge($countryCode,$weight)
    {
        $soap = new SoapClient(self::ORDERS_OPERATION_URLS, array ('encoding' => 'UTF-8' ));
         $arrs=[
         'countryCode'=>$countryCode,
         'weight'=>$weight,
         ];
        $result = $soap->calculateCharge(self::AUTHTOKEN,$arrs);
        return $result;
    }
}
?>